using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public class TypeUtil
    {
        //public static List<Assembly> GetApplicationAssemblies()
        //{
        //    List<Assembly> assemblies=new List<Assembly>();
        //    string appFolderPath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
        //    DirectoryInfo binFolderInfo=new DirectoryInfo(appFolderPath);
        //    FileInfo[] dllFileInfos = binFolderInfo.GetFiles("*.dll", SearchOption.TopDirectoryOnly);
        //    if(dllFileInfos !=null && dllFileInfos.Length>0)
        //    {
        //        foreach(FileInfo dllInfo in dllFileInfos)
        //        {
        //            Assembly assembly = Assembly.LoadFile(dllInfo.FullName);
        //            assemblies.Add(assembly);
        //        }
        //    }
        //    return assemblies;
        //}

        //public static object CreateObject(Assembly assembly, string typeFullName)
        //{
        //    List<Assembly> assemblies = TypeUtil.GetApplicationAssemblies();
        //    if(assemblies !=null && assemblies.Count>0)
        //    {
        //        foreach(Assembly assem in assemblies)
        //        {
        //            Type type = assem.GetType(typeFullName);
        //            if(type !=null)
        //            {
        //                return Activator.CreateInstance(type);
        //            }
        //        }
        //    }
        //    return null;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Dictionary<string,MemberInfo> GetReadableMembers(Type type)
        {
            Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
            FieldInfo[] fields = type.GetFields();
            foreach(FieldInfo field in fields)
            {
                members.Add(field.Name, field);
            }
            PropertyInfo[] props = type.GetProperties();
            foreach(PropertyInfo prop in props)
            {
                if(prop.CanRead)
                {
                    members.Add(prop.Name,prop);
                }
            }
            return members;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Dictionary<string,MemberInfo> GetWritableMembers(Type type)
        {
            Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
            FieldInfo[] fields = type.GetFields();
            foreach (FieldInfo field in fields)
            {
                members.Add(field.Name, field);
            }
            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.CanWrite)
                {
                    members.Add(prop.Name, prop);
                }
            }
            return members;
        }
    }
}
